cmake_minimum_required(VERSION 2.8.5)

message("=> Creating graphic module") 
if(MOD_FEM_VIEWER_USE_AS_LIB)
  message(STATUS "=> Graphic module status: external module")
else()
  message(STATUS "=> Graphic module status: standalone application")
endif()

# check if mode_fem_viewer can be build
# search for OpenGL
find_package(OpenGL REQUIRED)
if(OPENGL_FOUND)
  message(STATUS "=> OpenGL found successfully")
  set(BUILD_VIEWER TRUE)
else()
  message(ERROR " OpenGL not found")
  set(BUILD_VIEWER FALSE)
endif()

# search for FreeGLUT
find_package(GLUT REQUIRED)
if(GLUT_FOUND)
  message(STATUS "=> FreeGLUT found successfully")
  set(BUILD_VIEWER TRUE) 
else()
  message(ERROR " FreeGLUT not found!")
  set(BUILD_VIEWER FALSE)
endif()

#VTK for parallel rendering
if(${MODFEM_MPI} STREQUAL "mpi")
  find_package(VTK REQUIRED)
  if(VTK_FOUND)
    message(STATUS "=> VTK found successfully")
    set(BUILD_VIEWER TRUE) 
  else()
    message(ERROR " VTK not found!")
    set(BUILD_VIEWER FALSE)
  endif()
endif()


# search for GLEW



# search components for specifiedtype of GUI
# set defaults:
if(WIN32)
  # set defaults
  set(GUI_TYPE "WIN")
  if(MOD_FEM_VIEWER_GUI_WIN STREQUAL "WX")
    find_package(wxWidgets COMPONENTS base core gl REQUIRED)
	if(wxWidgets_FOUND)
	  message(STATUS "GUI bases on wxWidgets libs")
	  set(GUI_TYPE "wxWidgets")
	else()
	  message(FATAL_ERROR "Can't localize the WxWidghets")
	  set(BUILD_VIEWER FALSE)
	endif()
  elseif(MOD_FEM_VIEWER_GUI_WIN STREQUAL "GLT")
  # seracgh for other type is optional because win32 is allways supported and FreeGLUIT should be found before
    set(GUI_TYPE "GLT")
  endif()
elseif(UNIX)
  # set defaults
  set(GUI_TYPE "wxWidgets")
  if(MOD_FEM_VIEWER_GUI_LIN STREQUAL "WX")
    find_package(wxWidgets COMPONENTS base core gl REQUIRED)
	if(wxWidgets_FOUND)
	  message(STATUS "GUI bases on wxWidgets libs")
	else()
	  message(FATAL_ERROR "Can't localize the WxWidghets")
	  set(BUILD_VIEWER FALSE)
	endif()
  elseif(MOD_FEM_VIEWER_GUI_LIN STREQUAL "GLT")
  # seracgh for other type is optional because win32 is allways supported and FreeGLUIT should be found before
    set(GUI_TYPE "GLT")
  endif()
endif()


# TODO:  This involves some erros
#search for GLEW
find_package(GLEW REQUIRED)
if(GLEW_FOUND)
  message("GLEW found successfully")
  #include( ${GLEW_INCLUDE_PATH})
  set(BUILD_VIEWER TRUE)
else()
  set(BUILD_VIEWER FALSE)
endif()

if(BUILD_VIEWER)
  set(my_current_dir ${CMAKE_CURRENT_BINARY_DIR})
  set(my_current_lib_dir "${my_current_dir}/FemViewer")
  set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${my_current_lib_dir})
  
  set(mod_fem_viewer_srcdir "${CMAKE_SOURCE_DIR}/mod_fem_viewer" CACHE INTERNAL "")
  list(APPEND MOD_FEM_VIEWER_INCLUDE_DIRS
    ${mod_fem_viewer_srcdir}
    ${mod_fem_viewer_srcdir}/include
  )
  if(MOD_FEM_VIEWER_DIRS)
    set(MOD_FEM_VIEWER_DIRS ${MOD_FEM_VIEWER_DIRS} PARENT_SCOPE)
  endif()
  include_directories(${MOD_FEM_VIEWER_INCLUDE_DIRS} ${MOD_FEM_VIEWER_EXTERNAL_INCLUDE_DIRS} ${CMAKE_SOURCE_DIR}/include)
  

  add_subdirectory(utils)
  add_subdirectory(FemViewer)
  if(NOT MOD_FEM_VIEWER_USE_AS_LIB)
    add_subdirectory(ApproxSTDhybrid)
    add_subdirectory(IOManager)
    add_subdirectory(ApproxDG)
    add_subdirectory(ApproxSTD)
    add_subdirectory(ApproxModule) 
  endif()

else()
  message(FATAL_ERROR "=> ERROR: some required components could't be found to build graphic viewer!") 
endif()

